import { reqYuepiaoRank, reqRecommendBestSell, reqRecommendNewBook, reqBdRank, reqSearchBook, reqBookinfo, reqChapters, reqContent } from '@/api'
import { group } from '@/utils/help'
const novel = {
  state () {
    return {
      qidianImagBase:'https://bookcover.yuewen.com/qdbimg/349573/bid/150',
      bdRank: [],
      yuepiaoRank: [],
      recommendBestSell: [],
      recommendNewBook: [],
      searchList: [],
      searchName: '',
      bookinfo: {},
      chapters: [],
      content: [],
      showCatalog: false
    }
  },
  mutations: {
    SETBDRANK (state, data) { state.bdRank = data },
    SETYUEPIAORANK (state, data) { state.yuepiaoRank = data },
    SETBESTSELL (state, data) { state.recommendBestSell = data },
    SETNEWBOOK (state, data) { state.recommendNewBook = data },
    SEARCHLIST (state, data) { state.searchList = data },
    SEARCHNAME (state, data) { state.searchName = data },
    BOOKINFO (state, data) { state.bookinfo = data },
    CHAPTERS (state, data) { state.chapters = data },
    CONTENT (state, data) { state.content = data },
    SHOWCATALOG (state, data) { state.showCatalog = data }

  },
  actions: {
    getYuepiao: async ({ commit }, params) => {
      try {
        const res = await reqYuepiaoRank(params)
        if (res.code === 200) {
          commit('SETYUEPIAORANK', res.data.records)
        }
      } catch (error) {
        console.log(error)
      }
    },
    recommendBestSell: async ({ commit }, params) => {
      try {
        const res = await reqRecommendBestSell(params)
        if (res.code === 200) {
          commit('SETBESTSELL', res.data.records)
        }
      } catch (error) {
        console.log(error)
      }
    },
    recommendNewBook: async ({ commit }, params) => {
      try {
        const res = await reqRecommendNewBook(params)
        if (res.code === 200) {
          commit('SETNEWBOOK', res.data.records)
        }
      } catch (error) {
        console.log(error)
      }
    },
    bdRank: async ({ commit }, params) => {
      try {
        const res = await reqBdRank(params)
        if (res.code === 200) {
          commit('SETBDRANK', res.data.cards[0].content)
        }
      } catch (error) {
        console.log(error)
      }
    },
    searchBook: async ({ commit }, searchKey) => {
      try {
        commit('SEARCHLIST', [])
        commit('SEARCHNAME', searchKey)
        const res = await reqSearchBook(searchKey)
        if (res.code === 200) commit('SEARCHLIST', res.searchList)
      } catch (error) {
        console.log(error)
      }
    },
    bookinfo: async ({ commit }, source) => {
      try {
        const res = await reqBookinfo(source)
        if (res.code === 200) commit('BOOKINFO', res.data)
      } catch (error) {
        console.log(error)
      }
    },
    chapters: async ({ commit }, source) => {
      try {
        const res = await reqChapters(source)
        if (res.code === 200) commit('CHAPTERS', res.data)
      } catch (error) {
        console.log(error)
      }
    },
    content: async ({ commit, state }, { source, replace }) => {
      try {
        if (replace) commit('CONTENT', [])
        const res = await reqContent(source)
        if (res.code === 200){
          if (replace){
            commit('CONTENT', [res.data])
          } else {
            const content = state.content
            content.push(res.data)
            commit('CONTENT', content)
          }
        }
      } catch (error) {
        console.log(error)
      }
    }

  },
  getters: {
    yuepiaoTab (state) {
      return group(state.yuepiaoRank, 5)
    }
  }
}
export default novel
